<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-block">
          <input type="radio" name="type" lay-filter="type" value="0" title="模块">
          <input type="radio" name="type" lay-filter="type" value="1" title="目录" >
          <input type="radio" name="type" lay-filter="type" value="2" title="菜单" checked="">
          <input type="radio" name="type" lay-filter="type" value="3" title="按钮">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">名称</label>
        <div class="layui-input-block">
            <input type="text" name="title" id="title" lay-verify="required" lay-reqtext="模块,目录,菜单,按钮名称不能为空" placeholder="模块,目录,菜单,按钮名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" id="href1">
        <label class="layui-form-label required">菜单url</label>
        <div class="layui-input-block">
            <input type="text" name="href" id="href" lay-verify="required" lay-reqtext="菜单url不能为空" placeholder="请输入菜单url" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" id="parentid1">
        <label class="layui-form-label">父组别</label>
        <div class="layui-input-block">
            <input type="text" id="parentid" name="parentid" lay-filter="parentid" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" id="authcode1">
        <label class="layui-form-label">授权标识</label>
        <div class="layui-input-block">
            <input type="text" name="authcode" id="authcode" placeholder="授权标识" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" pane="">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="1" title="有效" checked="">
            <input type="radio" name="status" value="0" title="无效">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-radius layui-btn-primary" lay-submit lay-filter="saveBtn" id="saveBtn">确认保存</button>
            <button class="layui-btn layui-btn-radius layui-btn-primary"  id="backBtn">返回</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js" charset="utf-8"></script>
<script src="../../js/custorm/init.js"></script>
<script>
    layui.use(['form','treeSelect','iconPickerFa'], function () {
        var form = layui.form,
         iconPickerFa = layui.iconPickerFa,
            layer = layui.layer,treeSelect=layui.treeSelect,
            $ = layui.$;
        
        HavePermission('menu-add')==true?$("#saveBtn").show():$("#saveBtn").hide();
        var menu={parentid:0,parentname:"",href:"",title:"",status:true,authcode:"",icon:"",mType:0,id:0};
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            if((data.field.mType==1||data.field.mType==2||data.field.mType==3)&&menu.parentid==0){
                layer.msg("父类型不能为空！");
                return;
            }
            menu.authcode=data.field.authcode;
            menu.href=data.field.href;
            menu.status=data.field.status=="1"?true:false;
            menu.title=data.field.title;
                AxajPost('../../api/sys/AddMenu',menu,'保存成功','../../login.html');
        });
        form.on('radio(type)', function (data) {
                if(data.value==0){//模块
                    $("#href1").hide();
                    $("#href").attr("lay-verify","");
                    $("#parentid1").hide();
                    menu.parentid=0;
                    menu.mType=0;
                }else if(data.value==1)  {///目录
                    $("#href1").hide();
                    $("#href").attr("lay-verify","");
                    $("#parentid1").show();          
                    menu.mType=1;
                }else if(data.value==2)  {//菜单
                    $("#href1").show();
                    $("#href").attr("lay-verify","required");
                    $("#parentid1").show();
                    menu.mType=2;
                }else if(data.value==3)  {///按钮
                    $("#href1").hide();
                    $("#href").attr("lay-verify","");
                    $("#parentid1").show();
                    menu.mType=3;
                }            
        });

        $("#backBtn").click(function () {
            window.location.href="../menu.html";
        });

        treeSelect.render({
                // 选择器
                elem: '#parentid',
                // 数据
                data: '../../api/sys/GetAllMenuTree',
                // 异步加载方式：get/post，默认get
                type: 'get',
                // 占位符
                placeholder: '请选择父组别',
                // 是否开启搜索功能：true/false，默认false
                search: true,
                async: {
                    enable: false
                },
                // 点击回调
                click: function (d) {
                   //parentguid=
                    menu.parentid = parseInt(d.current.id);
                    menu.parentname = d.current.name;
                    console.log(d);
                },
                // 加载完成后的回调函数
                    success: function (d) {
                    treeSelect.checkNode('parentid', 1);
                    treeSelect.refresh();
                }
            });
    });
</script>
</body>
</html>